# Maintainer: Frode Solheim <frode@fs-uae.net>
# Contributor: Duong Pham <dthpham@gmail.com>
# Contributor: Lara Maia <dev@lara.click>

_realname=cx_Freeze
pkgbase=mingw-w64-python-${_realname}
pkgname=("${MINGW_PACKAGE_PREFIX}-python2-cx_Freeze"
         "${MINGW_PACKAGE_PREFIX}-python3-cx_Freeze")
pkgver=5.1.1
pkgrel=1
pkgdesc="Python package for freezing scripts into executables (mingw-w64)"
arch=('any')
license=('PSF')
url="https://cx-freeze.sourceforge.io/"
makedepends=("${MINGW_PACKAGE_PREFIX}-python2"
             "${MINGW_PACKAGE_PREFIX}-python3")
options=('staticlibs' 'strip' '!debug')
source=(cx_Freeze-${pkgver}.tar.gz::https://github.com/anthony-tuininga/cx_Freeze/archive/${pkgver}.tar.gz
        0001-fix-build.patch)
sha256sums=('d173234fd0973b0ed449865adb4234dd91566cfcd62a1894eb0f62709391e063'
            '9e59ad4303ef90b6e65f116befa28d5ef5542f4668de6289abb5638a50e9db9e')

prepare() {
  cd ${_realname}-${pkgver}
  patch -Np1 -i ${srcdir}/0001-fix-build.patch
  cd ..

  rm -Rf python2-cx_Freeze-${pkgver}-${CARCH}
  cp -a cx_Freeze-${pkgver} python2-cx_Freeze-${pkgver}-${CARCH}
  rm -Rf python3-cx_Freeze-${pkgver}-${CARCH}
  cp -a cx_Freeze-${pkgver} python3-cx_Freeze-${pkgver}-${CARCH}
}

build() {
  echo "Building for Python 2"
  cd python2-cx_Freeze-${pkgver}-${CARCH}
  ${MINGW_PREFIX}/bin/python2 setup.py build
  cd ..

  echo "Building for Python 3"
  cd python3-cx_Freeze-${pkgver}-${CARCH}
  ${MINGW_PREFIX}/bin/python3 setup.py build
  cd ..
}

package_python2-cx_Freeze() {
  depends=("${MINGW_PACKAGE_PREFIX}-python2"
           "${MINGW_PACKAGE_PREFIX}-python2-six")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})

  cd python2-cx_Freeze-${pkgver}-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python2 setup.py install --prefix=${MINGW_PREFIX} \
         --root="${pkgdir}" --optimize=1

  mv ${pkgdir}${MINGW_PREFIX}/bin/cxfreeze{,2}
  mv ${pkgdir}${MINGW_PREFIX}/bin/cxfreeze-postinstall{,2}
  mv ${pkgdir}${MINGW_PREFIX}/bin/cxfreeze-quickstart{,2}
  
  # fix python command in files
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for filename in cxfreeze2 cxfreeze-quickstart2; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${filename}
  done
  popd > /dev/null
}

package_python3-cx_Freeze() {
  depends=("${MINGW_PACKAGE_PREFIX}-python3"
           "${MINGW_PACKAGE_PREFIX}-python3-six")

  local _mingw_prefix=$(cygpath -am ${MINGW_PREFIX})
		   
  cd python3-cx_Freeze-${pkgver}-${CARCH}
  MSYS2_ARG_CONV_EXCL="--prefix=;--install-scripts=;--install-platlib=" \
    ${MINGW_PREFIX}/bin/python3 setup.py install --prefix=${MINGW_PREFIX} \
    --root="${pkgdir}" --optimize=1
	
  # fix python command in files
  pushd "${pkgdir}${MINGW_PREFIX}"/bin > /dev/null
  for filename in cxfreeze cxfreeze-quickstart; do
    sed -e "s|${_mingw_prefix}/bin/|/usr/bin/env |g" -i ${filename}
  done
  popd > /dev/null
}

package_mingw-w64-i686-python2-cx_Freeze() {
  package_python2-cx_Freeze
}

package_mingw-w64-i686-python3-cx_Freeze() {
  package_python3-cx_Freeze
}

package_mingw-w64-x86_64-python2-cx_Freeze() {
  package_python2-cx_Freeze
}

package_mingw-w64-x86_64-python3-cx_Freeze() {
  package_python3-cx_Freeze
}
